package com.sxkiler.demo.seata.service;

import com.sxkiler.demo.seata.dao.TCCOrderDao;
import com.sxkiler.demo.seata.dto.OrderDTO;
import com.sxkiler.demo.seata.service.tcc.TCCAccountService;
import com.sxkiler.demo.seata.service.tcc.TCCOrderService;
import io.seata.rm.tcc.api.BusinessActionContext;
import org.apache.dubbo.config.annotation.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@org.apache.dubbo.config.annotation.Service
@Service
public class TCCOrderServiceImpl implements TCCOrderService {


    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Reference
    TCCAccountService accountService;

    @Autowired
    private TCCOrderDao orderDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean orderTry(BusinessActionContext actionContext, Long orderId, Long userId, Long productId, Integer count, Integer payAmount) {
        String xId = actionContext.getXid();
        long branchId = actionContext.getBranchId();
        OrderDTO orderDTO = new OrderDTO(orderId, userId, productId, count, payAmount);
        logger.info("[orderTry]: 当前 XID:{}, branchId:{}, 订单:{}", xId, branchId, orderDTO.toString());
        // 扣减余额 Try
        boolean aTry = accountService.accountTry(actionContext, userId, payAmount);
        if(!aTry){
            throw new RuntimeException("账户服务 Try 阶段失败.");
        }

        //创建订单 Try
        int flag = orderDao.orderTry(orderDTO);
        if(flag == 0){
            throw new RuntimeException("订单服务 Try 阶段失败.");
        }

        logger.info("[orderTry]: 阶段成功");
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean orderConfirm(BusinessActionContext actionContext) {
        String xId = actionContext.getXid();
        long branchId = actionContext.getBranchId();
        Long orderId = ((Long) actionContext.getActionContext("orderId"));
        logger.info("[orderConfirm]: 当前 XID:{}, branchId:{}, 订单ID:{}", xId, branchId, orderId);

        int flag = orderDao.orderConfirm(orderId);
        if(flag == 0){
            throw new RuntimeException("订单服务 Confirm 阶段失败.");
        }
        logger.info("[orderConfirm]: 阶段成功");
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean orderCancel(BusinessActionContext actionContext) {
        String xId = actionContext.getXid();
        long branchId = actionContext.getBranchId();
        Long orderId = ((Long) actionContext.getActionContext("orderId"));
        logger.info("[orderCancel]: 当前 XID:{}, branchId:{}, 订单ID:{}", xId, branchId, orderId);

        int flag = orderDao.orderCancel(orderId);
        if(flag == 0){
//            throw new RuntimeException("订单服务 Cancel 阶段失败.");
        }
        logger.info("[orderCancel]: 阶段成功");
        return true;
    }
}
